﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Google.GData.Calendar;
using System.Collections;
using Google.GData.Extensions;
using Google.GData.Client;

namespace ACH
{
    public partial class Form1 : Form
    {
        const string URI = "https://www.google.com/calendar/feeds/default/private/full";
        const string FEED_URI = "https://www.google.com/calendar/feeds/hm67ovk5sn4b798in3n05635nc%40group.calendar.google.com/private/full";//"http://www.google.com/calendar/feeds/default";
        const string FEED_URI_PRIVATE = "https://www.google.com/calendar/feeds/hm67ovk5sn4b798in3n05635nc%40group.calendar.google.com/private-1a9865b48fec4a155adb39e63d3c8ff3/basic";
        const string FEED_ALL_CALS = "https://www.google.com/calendar/feeds/default/allcalendars/full";
        const string FEED_OWNED_CALS = "https://www.google.com/calendar/feeds/default/owncalendars/full";

        const string FEED_ACH_TEST = "https://www.google.com/calendar/feeds/hm67ovk5sn4b798in3n05635nc%40group.calendar.google.com/private/basic";
        public Form1()
        {
            InitializeComponent();
        }

        ArrayList entryList;

        private void btnConnect_Click(object sender, EventArgs e)
        {
            Cursor.Current = Cursors.WaitCursor;

            entryList = new ArrayList(50);
            ArrayList dates = new ArrayList(50);
            CalendarService service = new CalendarService("ACH");
            service.setUserCredentials(txtUser.Text, txtPassword.Text);

            FeedQuery queryAll = new FeedQuery();
            queryAll.Uri = new Uri(URI);//FEED_URI_PRIVATE);

            AtomFeed allCalFeed = service.Query(queryAll);

            var activeCalendar = allCalFeed.Entries.Where(x => x.Title.Text == "ACH Test").FirstOrDefault();


            EventQuery query = new EventQuery();
            query.Uri = new Uri(URI);
            query.StartTime = DateTime.Now.AddDays(-1);
            query.EndTime = DateTime.Now.AddMonths(1);
            EventFeed calFeed = service.Query(query) as EventFeed;

            calFeed = service.Query(queryAll) as EventFeed;//activeCalendar.Feed as EventFeed;
            // now populate the calendar
            while (calFeed != null && calFeed.Entries.Count > 0)
            {
                // look for the one with dinner time...
                foreach (EventEntry entry in calFeed.Entries)
                {
                    entryList.Add(entry);
                    if (entry.Times.Count > 0)
                    {
                        foreach (When w in entry.Times)
                        {
                            dates.Add(w.StartTime);
                        }
                    }
                }
                // just query the same query again.
                if (calFeed.NextChunk != null)
                {
                    queryAll.Uri = new Uri(calFeed.NextChunk);
                    calFeed = service.Query(queryAll) as EventFeed;
                }
                else
                    calFeed = null;
            }

            DateTime[] aDates = new DateTime[dates.Count];

            int i = 0;
            foreach (DateTime d in dates)
            {
                aDates[i++] = d;
            }


            monthCalendar1.BoldedDates = aDates;

            Cursor.Current = Cursors.Default;
        }

        private void monthCalendar1_DateSelected(object sender, DateRangeEventArgs e)
        {
            this.DayEvents.Items.Clear();

            ArrayList results = new ArrayList(5);
            foreach (EventEntry entry in this.entryList)
            {
                // let's find the entries for that date

                if (entry.Times.Count > 0)
                {
                    foreach (When w in entry.Times)
                    {
                        if (e.Start.Date == w.StartTime.Date ||
                            e.Start.Date == w.EndTime.Date)
                        {
                            results.Add(entry);
                            break;
                        }
                    }
                }
            }

            foreach (EventEntry entry in results)
            {
                ListViewItem item = new ListViewItem(entry.Title.Text);
                item.SubItems.Add(entry.Authors[0].Name);
                if (entry.Times.Count > 0)
                {
                    item.SubItems.Add(entry.Times[0].StartTime.TimeOfDay.ToString());
                    item.SubItems.Add(entry.Times[0].EndTime.TimeOfDay.ToString());
                }

                this.DayEvents.Items.Add(item);
            }
        }

        private void btnAddEvent_Click(object sender, EventArgs e)
        {

        }

        private void btnGetAll_Click(object sender, EventArgs e)
        {
            CalendarService myService = new CalendarService("exampleCo-exampleApp-1");
            myService.setUserCredentials(txtUser.Text, txtPassword.Text);

            CalendarQuery query = new CalendarQuery();
            query.Uri = new Uri(FEED_ACH_TEST);//FEED_OWNED_CALS);

            CalendarFeed resultFeed = (CalendarFeed)myService.Query(query);
            string cal = string.Empty;
            foreach (CalendarEntry entry in resultFeed.Entries)
            {
                cal += entry.Title.Text + "\r\n";
            }
            MessageBox.Show(cal, "Your calendars");
        }

        //get all events from ACH test, it works
        private void button1_Click(object sender, EventArgs e)
        {
            CalendarService service = new CalendarService("exampleCo-exampleApp-1");
            service.setUserCredentials(txtUser.Text, txtPassword.Text);

            FeedQuery query = new FeedQuery();
            query.Uri = new Uri(FEED_ACH_TEST);

            // Tell the service to query:
            AtomFeed calFeed = service.Query(query);
            string cal = string.Empty;
            for (int i = 0; i < calFeed.Entries.Count; i++)
            {
                cal += calFeed.Entries[i].Title.Text + "\r\n";
            }
            MessageBox.Show(cal, "Your calendars");
        }

        private void button2_Click(object sender, EventArgs e)
        {
            CreateRoomType rt = new CreateRoomType();
            rt.ShowDialog();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            CreateLodgingRoom l = new CreateLodgingRoom();
            l.ShowDialog();
        }

        private void button4_Click(object sender, EventArgs e)
        {
            CreateLodging l = new CreateLodging();
            l.ShowDialog();
        }

        private void button5_Click(object sender, EventArgs e)
        {
            CreateTrainingType tr = new CreateTrainingType();
            tr.ShowDialog();
        }

        //public EventEntry CreateEvent(CalendarService calendar, string title, DateTime date, AtomEntryCollection users)
        //{
        //    EventEntry entry = new EventEntry();
        //    entry.Title.Text = title;
        //    entry.Content.Content = title + "Content";
        //    When eventTime = new When(date, date + new System.TimeSpan(0, 0, 30, 0));
        //    entry.Times.Add(eventTime);
        //    foreach (AppsExtendedEntry user in users)
        //    {
        //        string member = user.getPropertyValueByName("memberId");
        //        Who who = new Who();
        //        who.Email = member;
        //        who.Rel = Who.RelType.EVENT_ATTENDEE;
        //        entry.Participants.Add(who);
        //    }
        //    Uri postUri = new Uri(string.Format(feedUrl, admin));
        //    return calendar.Insert(postUri, entry);
        //}

    }
}
